package com.yc.ioc;

public class Demo1 {

    public static void main(String[] args) {
        Computer computer = new Computer();
        // Spring 通过注入 DI 将打印机注入到computer内
        Printer p1 = new  BlackWhitePrinter();
        Printer p2 = new  ColorPrinter();

        computer.printer = p1;  // DI 依赖注入
        computer.print("Spring Demo");

        computer.printer = p2;
        computer.print("Spring Demo");
    }

}

class Computer {
    // 不在程序中直接创建对象, 将对象的控制器权交由 IOC 容器, 就是控制反转
    Printer printer; // = new XxxxPrinter();
    void print(String text){
        printer.print(text);
    }
}

// 存储介质接口, 请定义 U盘 和 硬盘
interface Storage {
    void save(Object data);
}

interface Printer {
    void print(String text);
}

class BlackWhitePrinter implements Printer{
    public void print(String text){
        System.out.println("黑色文字:" + text);
    }
}

class ColorPrinter implements Printer{
    public void print(String text){
        System.out.println("彩色文字:" + text);
    }
}
